package com.example.myui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.myui.R;
import com.example.myui.activity.HunPaiActivity;

import java.util.ArrayList;

public class SwipeAdapter extends RecyclerView.Adapter<SwipeAdapter.Holder> {
    private Context mContext;
    private ArrayList<String> mDatas = new ArrayList<>();
    private LayoutInflater inflater = null;
    private onItemClick onItemClick;

    public void setOnItemClick(SwipeAdapter.onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public SwipeAdapter(Context context, ArrayList<String> strings) {
        this.mContext = context;
        this.mDatas = strings;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = inflater.inflate(R.layout.item_text, parent, false);
        Holder holder = new Holder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        holder.textView.setText(mDatas.get(position) + "~");

        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClick != null) {
                    onItemClick.onItemClickListener(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void clear() {
        if (null != mDatas && mDatas.size() != 0) {
            mDatas.clear();
        }
        notifyDataSetChanged();
    }

    public void setDatas(ArrayList<String> strings) {
        this.mDatas = strings;
        notifyDataSetChanged();
    }

    public class Holder extends RecyclerView.ViewHolder {
        TextView textView;

        public Holder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_content);
        }
    }

    public interface onItemClick {
        void onItemClickListener(int position);
    }

}
